package com.dc.modules.test.simple.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.dc.commons.base.controller.DcBaseController;
import com.dc.commons.persistence.embeddable.Dict;
import com.dc.commons.persistence.embeddable.Member;
import com.dc.commons.query.DcQuery;
import com.dc.commons.views.ModelAndViewBuilder;
import com.dc.modules.test.simple.domain.Simple;
import com.dc.modules.test.simple.service.SimpleService;

@Controller
@RequestMapping("/test/simple")
public class SimpleController extends DcBaseController{
	
	 @Autowired
     Environment env;
	 
	 @Autowired
	private SimpleService simpleService;
	
	@RequestMapping("/index")
	public String index(Pageable pageable){
		System.out.println("/test/simple/index.htm?"+pageable.getPageNumber()+","+pageable.getPageSize());
		simpleService.deleteAll();
		
		for(int i=0;i<10;i++){
			Simple s=new Simple();
			s.setId("wangjdc"+i);
			s.setName("wangjdc"+i);
			s.setOwnerKey(Member.valueOf("admin"));
			s.setHobby(Dict.valueOf("HOBBY|ABD"));
			simpleService.saveSimple(s);
		}
		
		
		
		return "/test/simple/index";
	}
	
	@RequestMapping("/testSimpleManager")
	public ModelAndView testSimpleManager(Simple simple){
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/test/simple/testSimpleManager")
				.result(simple)
				.build();
	}
	
	/**
	 *  Simple查询
	 * @param request
	 * @param dcQuery
	 * @return ModelAndView
	 */
	@RequestMapping("/queryTestSimple")
	public ModelAndView queryTestSimple(NativeWebRequest request,DcQuery dcQuery){
		
		List<Map<String,Object>> content=new ArrayList<Map<String,Object>>();
		for(int i=0;i<15;i++){
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("id", Integer.toString(i));
			map.put("invdate", "2007-10-02");
			map.put("name", "test"+i);
			map.put("note", "test"+i);
			map.put("amount", i+1);
			map.put("tax", i);
			map.put("closed", true);
			map.put("ship_via", "TN");
			map.put("total", i+100);
			content.add(map);
		}
		Page page=new PageImpl(content,dcQuery.getPageable(),100);
		return ModelAndViewBuilder.create()
				.success()
				.result(page)
				.build();
	}
	
	@RequestMapping("/jsonTest")
	public ModelAndView jsonTest(){
		Simple simple=new Simple("wangjd");
		return new ModelAndView("/test/simple/index", "modelName", simple);
	}
	
	@RequestMapping("/jsonTest2")
	@ResponseBody
	public Simple jsonTest2(){
		Simple simple=new Simple("wangjd");
		return simple;
	}
	
	@RequestMapping("/json1")
	public ModelAndView json1(){
		Simple simple=new Simple("wangjd");
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/test/simple/index")
				.result(simple)
				.build();
	}
	
	@RequestMapping("/json2")
	public ModelAndView json2(){
		Exception ex=null;
		int a=0;
		try{
			 a=1/0;
			
		}catch(Exception e){
			ex=e;
		}
		System.out.println(a);
		return ModelAndViewBuilder.create()
				.viewName("/test/simple/index")
				.error("错误", ex)
				.build();
	}
	
	@RequestMapping("/json3")
	public ModelAndView json3(){
		Simple simple=new Simple("wangjd");
		return ModelAndViewBuilder.create()
				.viewName("/test/simple/index")
				.result(simple)
				.build();
	}
}
